//
//  NoticeListViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/7/4.
//

import UIKit
import CollectionKit
import JXSegmentedView
import RxCocoa
import RxSwift

enum NoticeListType: String {
  case myNotice   = "我的公告"
  case published  = "已发布"
  case firtTrial  = "待初审"
  case retrial    = "待复审"
  case trialed    = "已复审"
}

/// 通知列表
class NoticeListViewController: UIViewController {

  private var collectionView = CollectionView()
  private let disposeBag = DisposeBag()
  var type: NoticeListType = .myNotice

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    view.backgroundColor = .designKit.background

    let filter = R.nib.noticeFilterContentView(owner: self)!
    filter.update(type: type)
    filter.backgroundColor = .designKit.secondaryBackground
    view.addSubview(filter)

    filter.snp.makeConstraints { make in
      make.top.right.left.equalToSuperview()
      make.height.equalTo(44)
    }

    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { make in
      make.top.right.left.equalToSuperview()
      make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
    }

    let dataSource = ArrayDataSource<RepairOrderModel>(data:[
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
    ])

    let viewSource = ClosureViewSource<RepairOrderModel, NoticeItemContentView> { model, index in
      return R.nib.noticeItemContentView(owner: self)!
    } viewUpdater: { item, model, index in
            item.type = self.type
    }

    let sizeSource = { (index: Int, data: RepairOrderModel, size: CGSize) in
      return CGSize(width: size.width, height: 82)
    }

    let itemProvider = BasicProvider<RepairOrderModel, NoticeItemContentView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(spacing: 0)
        .inset(by: .padding(.horizontal, 15))
    )

    itemProvider.animator = FadeAnimator()
    itemProvider.tapHandler = { (context) in
      self.jumpToDetail(index: context.index)
    }

    collectionView.provider = ComposedProvider(sections: [itemProvider])
  }

  func jumpToDetail(index: Int) {
    let vc = NoticeDetailViewController()
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)
  }


  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */

}

extension NoticeListViewController: JXSegmentedListContainerViewListDelegate {

  func listView() -> UIView {
    return self.view
  }
}
